package com.luo.partner.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.luo.common.controller.BaseController;
import com.luo.common.utils.PageUtils;
import com.luo.common.utils.Query;
import com.luo.common.utils.R;
import com.luo.partner.dao.model.TabRedRecord;
import com.luo.partner.dao.model.TabRedRecordTemp;
import com.luo.partner.service.TabRedRecordService;
import com.luo.partner.service.TabUsrsService;
import com.luo.system.domain.UserDO;
import com.luo.system.service.UserService;

@Controller
@RequestMapping("/partner/userRed")
public class TabUsrsRedController extends BaseController{

	@Autowired
    private TabRedRecordService tabRedRecordService;
	@Autowired
    private TabUsrsService tabUsrsService;

	@Autowired
	UserService userService;

    @GetMapping()
    @RequiresPermissions("userRed")
    String tabUsrs() {
        return "partner/userRed/list";
    }

    @ResponseBody
	@GetMapping("/list")
    @RequiresPermissions("userRed")
    public PageUtils list(@RequestParam Map<String, Object> params) {
    	UserDO user = getUser();
    	List<Long> inviteUserIdList = null;
    	if (user.getLevel() == 1) {
			// 找出他下面所有合伙人的userId
    		inviteUserIdList = userService.findInviteUserId(user.getUserId());
		}else if (user.getLevel() == 2) {
			inviteUserIdList = new ArrayList<Long>();
			inviteUserIdList.add(user.getUserId());
		}
    	if (inviteUserIdList != null && inviteUserIdList.size() > 0) {
    		params.put("inviteUserIdList", inviteUserIdList);
		}
    	List<Long> usrsIdList = tabUsrsService.findUsrsIdList(params);
    	if (usrsIdList != null && usrsIdList.size() > 0) {
    		params.put("usrsIdList", usrsIdList);
		}
    	
        //查询列表数据
        Query query = new Query(params);
        List<TabRedRecordTemp> list = tabRedRecordService.findRedList(query);

        int total = tabRedRecordService.countRedList(query);

        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
	}

    @GetMapping("/edit/{recordId}")
    @RequiresPermissions("userEdit")
    String edit(@PathVariable("recordId") Long recordId, Model model) {
    	TabRedRecord record = tabRedRecordService.get(recordId);
        model.addAttribute("record", record);
        return "partner/userRed/edit";
    }
    
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/updateUserRed")
    @RequiresPermissions("updateUserRed")
    public R updateUserRed(TabRedRecord record) {
		record.setUpdateTime(new Date());
		tabRedRecordService.update(record);
        return R.ok();
	}

}
